package ln.fxspringarchetype.controller.score;

import javafx.beans.binding.Bindings;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TextField;
import javafx.util.StringConverter;
import javafx.util.converter.NumberStringConverter;
import ln.fxspringarchetype.controller.BaseFxController;
import ln.fxspringarchetype.model.Course;
import ln.fxspringarchetype.model.Student;
import ln.fxspringarchetype.vo.ScoreVO;
import org.apache.logging.log4j.util.Strings;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;
import java.util.Random;

public class AddScoreController extends BaseFxController {
    private final ScoreVO scoreVO = new ScoreVO();

    @FXML
    private ChoiceBox<Student> studentNameBox;
    @FXML
    private ChoiceBox<Course> courseNameBox;
    @FXML
    private DatePicker examDateField;
    @FXML
    private TextField scoreField;

    public ScoreVO getScoreVO() {
        return scoreVO;
    }

    @Override
    public void initLayout() {
        scoreField.setText("0.0");
        examDateField.setValue(LocalDate.now());
    }

    @Override
    public void loadData() {
        scoreVO.setRecordId(new Random().nextInt(0, Integer.MAX_VALUE));
    }

    @Override
    public void setCellValueFactories() {
        studentNameBox.setConverter(new StringConverter<>() {
            @Override
            public String toString(Student student) {
                return Objects.nonNull(student) ? student.getName() : Strings.EMPTY;
            }

            @Override
            public Student fromString(String studentName) {
                return studentNameBox.getSelectionModel().getSelectedItem();
            }
        });

        courseNameBox.setConverter(new StringConverter<>() {
            @Override
            public String toString(Course course) {
                return Objects.nonNull(course) ? course.getName() : Strings.EMPTY;
            }

            @Override
            public Course fromString(String string) {
                return courseNameBox.getSelectionModel().getSelectedItem();
            }
        });
    }

    @Override
    public void setCellFactories() {

    }

    @Override
    public void createBindings() {
        scoreVO.studentProperty().bind(studentNameBox.getSelectionModel().selectedItemProperty());
        scoreVO.courseProperty().bind(courseNameBox.getSelectionModel().selectedItemProperty());

        Bindings.bindBidirectional(scoreField.textProperty(), scoreVO.scoreProperty(), new NumberStringConverter());
        examDateField.valueProperty().bindBidirectional(scoreVO.examDateProperty());
    }

    public void loadListData(List<Student> students, List<Course> courses) {
        studentNameBox.getItems().clear();
        studentNameBox.setItems(FXCollections.observableList(FXCollections.observableList(students)));
        studentNameBox.getSelectionModel().selectFirst();

        courseNameBox.getItems().clear();
        courseNameBox.setItems(FXCollections.observableList(FXCollections.observableList(courses)));
        courseNameBox.getSelectionModel().selectFirst();
    }
}
